//
//  CompanyCardViewController.m
//  ERP
//
//  Created by QA on 15/10/22.
//  Copyright © 2015年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "CompanyCardViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import "ZXingWrapper.h"

@interface CompanyCardViewController ()

@end

@implementation CompanyCardViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self setTitle:@"企业简码信息"];
        UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStyleDone target:self action:@selector(saveImageCode:)];
        self.navigationItem.rightBarButtonItem = leftButton;
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.edgesForExtendedLayout = UIRectEdgeAll;
    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor clearColor]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = ThemeColorBlue;
    
    //将图层的边框设置为圆脚
    self.barcodeView.layer.cornerRadius = 4;
    self.barcodeView.layer.masksToBounds = YES;
    
    //给图层添加一个有色边框
    self.barcodeView.layer.borderWidth = 0.5;
    self.barcodeView.layer.borderColor = [UIColor colorWithWhite:0.1 alpha:0.2].CGColor;
    
    //设置公司信息
    ConfigurationCodeing *model = [ConfigurationHelper getConfigurationData];
    NSMutableString *briefCode = [NSMutableString stringWithString:model.briefCode];
    for (int i= 0; i < briefCode.length; i++) {
        [briefCode insertString:@" " atIndex:i]; i++;
    }
    
    self.barcodeNumberLabel.text = [briefCode uppercaseString];
    self.companyNameLabel.text = model.organization;
    
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    ConfigurationCodeing *model = [ConfigurationHelper getConfigurationData];
    
    //生成二维码并显示
    CGSize imageSize = self.barcodeImageView.bounds.size;
    UIImage *imageCode =  [ZXingWrapper createCodeWithString:model.briefCode size:imageSize CodeFomart:kBarcodeFormatQRCode];
    self.barcodeImageView.image = imageCode ? imageCode : [UIImage new];
}

//保存企业简码（二维码）
- (void)saveImageCode:(id)selector
{
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, 0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    viewImage = [self addImageLogo:viewImage text:[UIImage imageNamed:@"login_bg_head"]];
    UIGraphicsEndImageContext();
    
    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
    AudioServicesPlaySystemSound(1108);
    [self showAlertView:@"照片已保存到手机相册，请前往相册查看"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)showAlertView:(NSString*)message
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"保存成功" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    alert.alertViewStyle=UIAlertViewStyleDefault;
    [alert show];
}

//2.加图片
-(UIImage *)addImageLogo:(UIImage *)img text:(UIImage *)logo
{
    CGFloat scaleFactor = [[UIScreen mainScreen] scale];
    CGFloat height = self.view.bounds.size.height*scaleFactor;
    
    //get image width and height
    int w = img.size.width*scaleFactor;
    int h = img.size.height*scaleFactor;
    int logoWidth = logo.size.width*scaleFactor;
    int logoHeight = logo.size.height*scaleFactor;
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
    CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
    CGContextDrawImage(context, CGRectMake(w/2-logoWidth/2, height - logoHeight, logoWidth, logoHeight), [logo CGImage]);
    
    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    return [UIImage imageWithCGImage:imageMasked];
}

//3.加半透明的水印
- (UIImage *)addImage:(UIImage *)useImage addImage1:(UIImage *)addImage1
{
    UIGraphicsBeginImageContext(useImage.size);
    [useImage drawInRect:CGRectMake(0, 0, useImage.size.width, useImage.size.height)];
    [addImage1 drawInRect:CGRectMake(0, useImage.size.height-addImage1.size.height, addImage1.size.width, addImage1.size.height)];
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultingImage;
}

@end
